package Model.SurroundModel;

import Model.DrawModel.CalPoint;

import java.util.ArrayList;

public class NewSampling {
    ArrayList<CalPoint> calPoints = new ArrayList<>();
    ArrayList<Target> targets = new ArrayList<>();
    ArrayList<TargetSecond> targetseconds = new ArrayList<>();
    private double BORDER = 0.98; //设置多少π可以认为两点共线（因为存在误差所以可能不为1）
    private double π = Math.PI;

    public static ArrayList<CalPoint> GetCircleAndTriangleShips(double distance) {
        ArrayList<CalPoint> result = new ArrayList<>();


        double radius = 50.0;

        for (double i = Math.PI; i >= 0; i -= distance * 0.05) {
            result.add(new CalPoint(radius * Math.cos(i), radius * Math.sin(i)));
        }

        for (double x = 50; x >= 0; x -= distance) {
            result.add(new CalPoint(x, x - 50));
        }
        for (double x = 0; x >= -50; x -= distance) {
            result.add(new CalPoint(x, -x - 50));
        }

        return result;
    }

    NewSampling() {
    }

    ;

    public double distance(double x1, double y1, double x2, double y2) {
        return Math.sqrt(Math.pow(x1 - x2, 2) + Math.pow(y1 - y2, 2));
    }

    public void Sampling(int pre_num, int total_num) {
        int mark[] = new int[total_num];  //标记要取点得位置
        double cosθ;
        double θ;
        int num = 0;
        int id = 0;
        //根据角度先取点
        for (int i = 0; i < calPoints.size(); i++) {
            //取首末两个点
            if (i == 0) {
                double d1 = distance(calPoints.get(total_num - 1).x, calPoints.get(total_num - 1).y, calPoints.get(i).x, calPoints.get(i).y);
                double d2 = distance(calPoints.get(i).x, calPoints.get(i).y, calPoints.get(i + 1).x, calPoints.get(i + 1).y);
                double d3 = distance(calPoints.get(total_num - 1).x, calPoints.get(total_num - 1).y, calPoints.get(i + 1).x, calPoints.get(i + 1).y);
                cosθ = (Math.pow(d1, 2) + Math.pow(d2, 2) - Math.pow(d3, 2)) / (2 * d1 * d2);
                θ = Math.acos(cosθ);
                //如果角度小于180*BORDER 将对应得mark值置为1
                if (θ < BORDER * π && mark[i] != 1) {
                    mark[i] = 1;
                    num++;
                }
            } else if (i == calPoints.size() - 1) {
                double d1 = distance(calPoints.get(i - 1).x, calPoints.get(i - 1).y, calPoints.get(i).x, calPoints.get(i).y);
                double d2 = distance(calPoints.get(i).x, calPoints.get(i).y, calPoints.get(0).x, calPoints.get(0).y);
                double d3 = distance(calPoints.get(i - 1).x, calPoints.get(i - 1).y, calPoints.get(0).x, calPoints.get(0).y);
                cosθ = (Math.pow(d1, 2) + Math.pow(d2, 2) - Math.pow(d3, 2)) / (2 * d1 * d2);
                θ = Math.acos(cosθ);
                //如果角度小于180*BORDER 将对应得mark值置为1
                if (θ < BORDER * π && mark[i] != 1) {
                    mark[i] = 1;
                    num++;
                }
            }
            //按角度取点
            else {
                double d1 = distance(calPoints.get(i - 1).x, calPoints.get(i - 1).y, calPoints.get(i).x, calPoints.get(i).y);
                double d2 = distance(calPoints.get(i).x, calPoints.get(i).y, calPoints.get(i + 1).x, calPoints.get(i + 1).y);
                double d3 = distance(calPoints.get(i - 1).x, calPoints.get(i - 1).y, calPoints.get(i + 1).x, calPoints.get(i + 1).y);
                cosθ = (Math.pow(d1, 2) + Math.pow(d2, 2) - Math.pow(d3, 2)) / (2 * d1 * d2);
                θ = Math.acos(cosθ);
                //如果角度小于180*BORDER 将对应得mark值置为1
                if (θ < BORDER * π && mark[i] != 1) {
                    mark[i] = 1;
                    num++;
                }
            }
        }
        if (num > pre_num) {
            //提示点数不足以将其显示
            System.out.println("1111");
            System.out.println("至少需要 " + num);
        }
        //计算取点得步长
        int remain_num = pre_num - num;
        System.out.println(remain_num);
        System.out.println(num);
        if (remain_num != 0) {
            int step = total_num / remain_num;
            System.out.println(step);
            for (int i = step; i < calPoints.size(); i += step) {
                if (mark[i] != 1) mark[i] = 1;
                else if (mark[i] == 1) {
                    if (mark[i+(int) step /2]!=1){
                        mark[i+(int) step /2]=1;
                    }else {
                        mark[i+(int) step /3]=1;
                    }
                }
            }
        }
        for (int i = 0; i < calPoints.size(); i++) {
            if (mark[i] == 1) {
                id++;
                System.out.println(calPoints.get(i));
                targets.add(new Target(id, calPoints.get(i).x, calPoints.get(i).y));
            }
        }
    }
}
